package com.android.gufengmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.OrderBean;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {

    private Context mContext;
    private List<OrderBean> mOrders;
    private IDeleteLongClick mIDeleteLongClick;

    public OrderAdapter(Context context, List<OrderBean> addresses) {
        mContext = context;
        mOrders = addresses;
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new OrderViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_order,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        OrderBean address = mOrders.get(position);
        holder.mTvOrderId.setText(address.getOrderId() + "");
        holder.mTvOrderInfo.setText(address.getOrderInfo());
        holder.mTvOrderPrice.setText(address.getOrderPrice() + "");
        holder.itemView.setOnLongClickListener(v -> {
            mIDeleteLongClick.onLongClickDelete(position);
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return mOrders.size();
    }

    public class OrderViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvOrderId;
        private final TextView mTvOrderInfo;
        private final TextView mTvOrderPrice;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvOrderId = itemView.findViewById(R.id.tv_id);
            mTvOrderInfo = itemView.findViewById(R.id.tv_info);
            mTvOrderPrice = itemView.findViewById(R.id.tv_price);
        }
    }

    public void setIDeleteLongClick(IDeleteLongClick longClick) {
        mIDeleteLongClick = longClick;
    }

    public interface IDeleteLongClick{
        void onLongClickDelete(int position);
    }
}
